import Vue from 'vue'
import VueRouter from 'vue-router'
// admin
const Login = () => import('../admincomponents/Login')
const Home = () => import('../admincomponents/Home')
const Welcome = () => import('../admincomponents/Welcome')
const Adminlist = () => import('../admincomponents/user/adminlist');
const Userlist = () => import('../admincomponents/user/userlist');
const Buslist = () => import('../admincomponents/user/Buslist')
const Catelist = () => import('../admincomponents/cate/cate')
const Goodslist = () => import('../admincomponents/goods/goods')
const Orderlist = () => import('../admincomponents/order/order')
// front
const Main = () => import('../front/mian');
const Index = () => import('../front/index')
const Cart = () => import('../front/cart')
const CatePart = () => import('../front/catepart')
// bussiness
const busLogin = () => import('../buscomponents/Login')
const busWelcome = () => import('../buscomponents/Welcome')
const busHome = () => import('../buscomponents/Home')
const busGoodslist = () => import('../buscomponents/goods/goods')
const busOrderlist = () => import('../buscomponents/order/order')
//导入进度条插件
import NProgress from 'nprogress'
//导入进度条样式
import 'nprogress/nprogress.css'
import nProgress from 'nprogress'
Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/main' },
  {
    path: '/main',
    component: Main,
    redirect: '/index',
    children: [
      {
        path: '/index',
        component: Index,
        meta: {
          title: '商城首页'
        },
      },
      {
        path: '/catepart',
        component: CatePart,
        meta: {
          title: '商品分类'
        },
      },
      {
        path: '/cart',
        component: Cart,
        meta: {
          title: '购物车'
        },
      },
    ]
  },
  { path: '/login', component: Login, meta: { title: '在线商城后台系统管理员登录' } },
  {
    path: '/home',
    component: Home,
    meta: {
      title: '主页'
    },
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        component: Welcome,
        meta: {
          title: '主页'
        },
      },
      {
        path: '/adminlist',
        component: Adminlist,
        meta: {
          title: '管理员列表'
        },
      },
      {
        path: '/userlist',
        component: Userlist,
        meta: {
          title: '用户列表'
        },
      },
      {
        path: '/buslist',
        component: Buslist,
        meta: {
          title: '商家列表'
        },
      },
      {
        path: '/catelist',
        component: Catelist,
        meta: {
          title: '分类列表'
        },
      },
      {
        path: '/goodslist',
        component: Goodslist,
        meta: {
          title: '商品列表'
        }
      },
      {
        path: '/orderlist',
        component: Orderlist,
        meta: {
          title: '订单列表'
        }
      }
    ]
  },
  { path: '/buslogin', component: busLogin, meta: { title: '在线商城商家后台管理' } },
  {
    path: '/bushome',
    component: busHome,
    redirect: '/buswelcome',
    children: [
      {
        path: '/buswelcome',
        component: busWelcome,
        meta: { title: '首页' }
      },
      {
        path: '/busgoodslist',
        component: busGoodslist,
        meta: { title: '商品列表' }
      },
      {
        path: '/busorderlist',
        component: busOrderlist,
        meta: { title: '订单管理' }
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  NProgress.start();
  if (to.path === '/login')
    return next();
  if (to.path === '/buslogin')
    return next();
  if (to.path === '/main')
    return next();
  if (to.path === '/index')
    return next();
  if (to.path === '/catepart')
    return next();
  if (to.path === '/cart')
    return next();
  //获取token
  const tokenStr = window.sessionStorage.getItem('token');
  if (!tokenStr) {
    nProgress.done();
    return next('/main');
  }


  next();

})

router.afterEach(() => {
  NProgress.done();
  // 在即将进入新的页面组件前，关闭掉进度条
})

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

export default router
